package com.scpii.universal.pns.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.scpii.universal.pns.dao.ResourcesDao;
import com.scpii.universal.pns.entity.Resources;

@Service
public class ResourcesServiceImpl extends PnsBaseServiceImpl<Resources, Long>
		implements ResourcesService {
	@Resource
	private ResourcesDao resourcesDao;

	@Resource
	public void setBaseDao(ResourcesDao resourcesDao) {
		super.setBaseDao(resourcesDao);
	}

	private Log log = LogFactory.getLog(this.getClass());

	/**
	 * 相册管理
	 * 
	 * @param targetId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public List<Resources> getPhotoInfo(Long targetId, Integer pageNo,
			Integer pageSize) {
		List<Resources> list = null;
		// 取得用户的相册
		StringBuffer hql = new StringBuffer("select r from Resources r");

		hql.append(" where r.targetId = :targetId order by r.createDt desc");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("targetId", targetId);
		list = resourcesDao.findByHql(hql.toString(), pageNo, pageSize, map,
				null);

		return list;

	}

	public Integer getPhotoInfoCount(Long targetId) {
		StringBuffer hql = new StringBuffer("select count(*) from Resources r");
		hql.append(" where r.targetId = :targetId");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("targetId", targetId);
		List<Long> list = resourcesDao.findByHqlCount(hql.toString(), 0, 0,
				map, null);
		if (list != null && list.size() > 0) {
			return list.get(0).intValue();
		} else {
			return 0;
		}

	}

	public Resources getResById(String resId) {
		StringBuffer hql = new StringBuffer("select r from Resources r");

		hql.append(" where r.id = :resId");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("resId", Long.parseLong(resId));
		List<Resources> list = resourcesDao.findByHql(hql.toString(), 0, 0,
				map, null);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public void insertRes(Resources resources) {
		try {
			resourcesDao.save(resources);
		} catch (Exception e) {
			log.error("插入资源表失败");
			log.error(e.getMessage());
		}
	}

	public int updateRes(Resources resources) {
		try {
			Resources res = getResById(resources.getId().toString());
			res.setMediaUrl(resources.getThumbImage());
			res.setThumbImage(resources.getThumbImage());
			res.setTitle(resources.getTitle());
			resourcesDao.update(res);
			return 0;
		} catch (Exception e) {
			log.error("修改资源表失败");
			log.error(e.getMessage());
			return 1;
		}
	}

	/**
	 * 删除照片
	 * 
	 * @param resId
	 * @param absolute
	 * @throws Exception
	 */
	public void deletePhoto(String resId, String absolute) throws Exception {
		String hql = "select t from Resources t where t.id="
				+ Long.parseLong(resId);
		List<Resources> photoInfo = resourcesDao.findByHql(hql, 0, 0, null,
				null);
		if (photoInfo != null && photoInfo.size() > 0) {
			Resources resources = photoInfo.get(0);
			// hql = "delete from Resources t where t.id=" +
			// Long.parseLong(resId);
			Map<String, Object> argMap = new HashMap<String, Object>();
			argMap.put("id", Long.parseLong(resId));
			resourcesDao.updateByHql("DELETE_RES_BY_ID", argMap);
			String url = resources.getMediaUrl();
			File imgFile = new File(absolute + url);
			if (imgFile.exists()) {
				imgFile.delete();
			}
		}
	}

	/**
	 * 删除相冊
	 * 
	 * @param resId
	 * @param absolute
	 * @throws Exception
	 */
	public void deletePhotoAlbums(String activitiesId, String absolute,
			String userId, String title) throws Exception {
		String hql = "select t from Resources t where t.targetId="
				+ Long.parseLong(activitiesId) + " and t.createBy="
				+ Long.parseLong(userId) + "and t.title='" + title + "'";
		List<Resources> photoInfo = resourcesDao.findByHql(hql, 0, 0, null,
				null);
		if (photoInfo != null && photoInfo.size() > 0) {
			for (Resources resources : photoInfo) {
				// hql = "delete from Resources t where t.id=" +
				// Long.parseLong(resId);
				Map<String, Object> argMap = new HashMap<String, Object>();
				argMap.put("id", resources.getId());
				resourcesDao.updateByHql("DELETE_RES_BY_ID", argMap);
				String url = resources.getMediaUrl();
				File imgFile = new File(absolute + url);
				if (imgFile.exists()) {
					imgFile.delete();
				}
			}
		}
	}

	@Override
	public void updateResById(Long resId) {
		// TODO Auto-generated method stub
		resourcesDao.delete(resId);

	}

	@Override
	public List<Resources> getAllByTargerId(Long targetId) {
		List<Resources> list = null;
		StringBuffer hql = new StringBuffer("select r from Resources r");
		hql.append(" where r.targetId = :targetId order by r.createDt desc");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("targetId", targetId);
		list = resourcesDao.hqlGetList(hql.toString(), map);
		return list;
	}

}
